<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class TB_News extends CI_Model {	
	function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

	function Insert($NewsEnt)
	{
		$this->db->insert('TB_News', $NewsEnt); 
	}
	function Update($NewsEnt)
	{
		$this->db->update('TB_News', $NewsEnt,array('NewsID' => $NewsEnt['NewsID']));
	
	}
	function Delete($NewsID)
	{
		$this->db->delete('TB_News', array('NewsID' => $NewsID)); 
	
	}
	function UpdateHits($Hits,$NewsID)
	{
		$this->db->update('TB_News', $Hits,array('NewsID' =>$NewsID));
	}
	function UpdateImgPath($ImgPath,$NewsID)
	{
		$this->db->update('TB_News', $ImgPath,array('NewsID' =>$NewsID));
	}
	function UpdateOrderNum($OrderNum,$NewsID)
	{
		$this->db->update('TB_News', $OrderNum,array('NewsID' => $NewsID));
	}
	
	 function get_last_Ten_entries($ClassName)
    {
    	$sql = "SELECT * FROM TB_News WHERE ClassName = ? order by OrderNum asc,AddTime asc limit 0, 10 "; 
		$query =$this->db->query($sql, array($ClassName)); 
        //$query = $this->db->get('TB_News', 5);
        //$songsQuery = $this->db->where('ClassName', $ClassName);
        //$query =$this->db->order_by("OrderNum"); 
        return $query->result();
    }
     function get_NewsByClassID($ClassID)
    {
    	$sql = "SELECT NewsID,TypeID,ClassID,ClassName,TypeName,Title,NewsSource,OrderNum ,AddTime,ImgPath 
    	FROM TB_News WHERE ClassID = ? order by ClassID asc,TypeID asc,  OrderNum asc,AddTime asc"; 
		$query =$this->db->query($sql, array($ClassID));
        return $query->result();
    }
    function get_NewsByType($ClassID,$TypeID)
    {
    	$sql = "SELECT NewsID,TypeID,ClassID,ClassName,TypeName,Title,NewsSource,OrderNum, AddTime
    	FROM TB_News WHERE ClassID = ? and TypeID= ? order by ClassID asc,TypeID asc, OrderNum asc,AddTime asc"; 
		$query =$this->db->query($sql, array($ClassID,$TypeID));
        return $query->result();
    }
     function get_News($num, $offset)
   {
     	$this->db->select('NewsID,Title,NewsSource,OrderNum');
	    $query = $this->db->get('TB_News', $num, $offset);        
	    return $query;
  }
   function get_NewsByNewsID($NewsID)
   {
     	$sql = "SELECT * FROM TB_News WHERE NewsID = ?"; 
		$query =$this->db->query($sql, array($NewsID));
        return $query->result(); 
  }
   function get_MaxNewsID()
   {
     	$sql = "SELECT Max(NewsID) NewsID FROM TB_News"; 
		$query =$this->db->query($sql);
        return $query->result(); 
  }

}
?>